package com.example.lanou.mynews.thread;

import android.os.Handler;
import android.os.Message;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.example.lanou.mynews.beans.AllDataBeans;
import com.example.lanou.mynews.single.SingleMode;
import com.google.gson.Gson;

/**
 * Created by dllo on 16/1/25.
 */
public class AllDataBeansThread extends Thread {
    public String tid ;
    /**
     * 格式  0-10；
     */
    public String size;
    public String url ;
    private  AllDataBeans allData;
    private Handler handler;

    /***
     *
     * @param tid
     * @param size  格式 0-10
     */
    public AllDataBeansThread(String tid, String size,Handler handler) {
        this.tid = tid;
        this.size = size;
        this.handler=handler;
        url= "http://c.3g.163.com/nc/article/headline/"+tid+"/"+size+".html";
    }

    @Override
    public void run() {
        super.run();

        StringRequest data = new StringRequest(url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                Gson gson = new Gson();
                allData=gson.fromJson(response, AllDataBeans.class);
                Message msg = new Message();
                msg.what=1;
                msg.obj=allData;
                handler.sendMessage(msg);
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                //TODO 这里请求失败，发送一个空数据
                handler.sendEmptyMessage(3);

            }
        });
        SingleMode.getInstance()._addRequest(data);



    }
}
